package com.example.carrierapp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DispatchAgainDialog extends Dialog {

    @BindView(R.id.et_modify_volume)
    TextView mTvCarNum;
    @BindView(R.id.tv_agin_name)
    TextView mTvDriverName;
    @BindView(R.id.tv_agin_phone)
    TextView mTvDriverPhone;
    @BindView(R.id.et_modify_carnum)
    EditText mEtVolume;

    private int mId;

    public DispatchAgainDialog(@Nullable Context context) {
        super(context, R.style.float_dialog);
        setContentView(R.layout.dialog_dispatch_again);
        ButterKnife.bind(this);
        initParameter();
        initView();
        setCanceledOnTouchOutside(true);

    }

    private void initView() {
    }

    public void showDialog(String driverName, String driverPhone, int carId, String carNum) {
//        mTvDes.setText(Html.fromHtml("截止" + time + " 前可修改装车重量，逾期不可再做修改。"));
        mId = carId;
        mEtVolume.setText("");
        mTvCarNum.setText(carNum);
        mTvDriverName.setText(driverName);
        mTvDriverPhone.setText(TextUtils.isEmpty(driverPhone) ? "暂无电话" : driverPhone);
        this.show();
    }

    @OnClick({R.id.ib_modify_close})
    public void onCloseClick(View v) {
        dismiss();
    }

    @OnClick({R.id.et_modify_volume})
    public void onCarNumClick(View v) {
        if (mOnVolumeClickListeber != null) {
            mOnVolumeClickListeber.onCarnumClick();
        }

    }

    @OnClick(R.id.tv_modify_sure)
    public void onSureClick() {
        if (!TextUtils.isEmpty(mTvCarNum.getText().toString().trim()) || !TextUtils.isEmpty(mEtVolume.getText().toString().trim())) {
            if (mOnVolumeClickListeber != null) {
                String volume = mEtVolume.getText().toString().trim();
                if (TextUtils.isEmpty(volume)) {
                    volume = "";
                } else if (Double.valueOf(volume) > 120 || Double.valueOf(volume) <= 0) {
                    ToastUtils.showCenterToast(getContext(), getContext().getResources().getString(R.string.hint_dispatch_again_carnum));
                    return;
                }
                mOnVolumeClickListeber.onAgainClick(mId, volume);
            }
        } else {
            ToastUtils.showCenterToast(getContext(), "未改变数据，不能点击！");
        }
    }

    private void initParameter() {
        Window window = getWindow(); // 得到对话框
        WindowManager.LayoutParams wl = window.getAttributes();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        wl.width = d.getWidth() - DisplayUtil.dip2px(getContext(), 50);
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }

    public void setCarNum(int id, String carNum) {
        mId = id;
        mTvCarNum.setText(carNum);

    }

    public interface OnVolumeClickListeber {
        void onAgainClick(int carId, String volume);

        void onCarnumClick();
    }

    private OnVolumeClickListeber mOnVolumeClickListeber;

    public void setOnVolumeClickListeber(OnVolumeClickListeber listeber) {
        mOnVolumeClickListeber = listeber;
    }
}
